import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';

class GetXTemplateGenerateController extends BaseController {
  final code = ''.obs;
  final isContainTitle = true.obs;

  TextEditingController _textEditingController = TextEditingController();

  TextEditingController get textEditingController => _textEditingController;

  TextEditingController _codeTextEditingController = TextEditingController();

  TextEditingController get codeTextEditingController =>
      _codeTextEditingController;

  TextEditingController _titleEditingController = TextEditingController();

  TextEditingController get titleTextEditingController =>
      _titleEditingController;

  String transformString(String input) {
    if (input.length <= 1) {
      return input;
    }

    // 取出第一个字符
    String firstChar = input[0];
    // 从第二个字符开始处理
    String transformed = input.substring(1).split('').map((char) {
      int codeUnit = char.codeUnitAt(0);
      if (codeUnit >= 'A'.codeUnitAt(0) && codeUnit <= 'Z'.codeUnitAt(0)) {
        return '_${char.toLowerCase()}';
      }
      return char;
    }).join('');

    // 将第一个字符与处理后的字符串拼接
    return firstChar.toLowerCase() + transformed;
  }

  generate() {
    String stateName = _textEditingController.text;
    String controllerImportInfo = transformString(stateName);
    controllerImportInfo =
        'import \'${controllerImportInfo}_controller.dart\'' + '\;\n\n';
    String title = _titleEditingController.text.trim().toString();
    String test = 'import \'package:get/get.dart\'';
    code.value = 'import \'package:flutter/material.dart\'\;\n' +
        'import \'package:get/get.dart\'\;\n\n' +
        controllerImportInfo +
        'class ' +
        stateName +
        'Page extends StatelessWidget \{\n\n' +
        '@override\n' +
        'Widget build(BuildContext context) {\n' +
        'return GetBuilder<\n' +
        _firstWordToUpper(stateName) +
        'Controller>(\n' +
        'init: ' +
        _firstWordToUpper(stateName) +
        'Controller(),\n' +
        'builder: (controller) {\n' +
        'return Scaffold(' +
        (isContainTitle.value
            ? ' appBar: AppBar(' + 'title: Text(' '\'' + title + '\'' + '))'
            : '') +
        ');});}}';

    _codeTextEditingController.text = code.value;
  }

  int findFirstUpperCaseIndex(String str) {
    for (int i = 0; i < str.length; i++) {
      if (str[i] == str[i].toUpperCase() && str[i].isAlphabetOnly) {
        return i;
      }
    }
    return -1; // 或者返回一个表示未找到的值
  }

  _firstWordToUpper(String text) {
    return text.substring(0, 1).toUpperCase() + text.substring(1);
  }

  copyCode() {
    copyToClipboard(_codeTextEditingController.text);
    toast('复制成功');
  }

  clear() {
    _codeTextEditingController.clear();
  }

  // 复制到剪贴板的方法
  Future<void> copyToClipboard(String text) async {
    await Clipboard.setData(ClipboardData(text: text)); // 设置剪贴板数据
  }

  changeState() {
    isContainTitle.value = !isContainTitle.value;
  }
}
